package org.tiny.sequence.rest;

import org.tiny.sequence.server.OrderNoService;
import org.tiny.sequence.server.SequenceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author MMM
 * @desc 获取序列号
 * @date 2017-01-23 15:47
 **/
@RestController
public class SequenceCtrl {

  @Autowired
  private SequenceService sequenceService;
  @Autowired
  private OrderNoService orderNoService;

  /**
   * 获取单个序列号
   *
   * @return
   */
  @GetMapping(value = "/next-id", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public Map<String, Object> getId() {
    long id = sequenceService.nextId();
    Map<String, Object> result = new HashMap();
    result.put("id", id);
    return result;
  }

  /**
   * 获取多个序列号
   *
   * @param num
   * @return
   */
  @GetMapping(value = "/next-id/{num}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public Map<String, Object> getIds(@PathVariable(name = "num", required = true, value = "num") String num) {
    long[] ids = sequenceService.nextIds(Integer.parseInt(num));
    Map<String, Object> result = new HashMap<String, Object>();
    result.put("ids", ids);
    return result;
  }

  /**
   * 获取格式化的订单号 时间（170123160440）+6位随机数
   * @return
   */
  @GetMapping(value = "/next-id/format", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public Map<String,Object> getFormatIds(){
    Map<String,Object> result = new HashMap<>();
    String id = orderNoService.next();
    result.put("id", id);
    return result;
  }

}
